/**
 * @author QinGuan
 * @date:2016-1-5 下午05:01:27
 * @version :3.0
 *
 */
package com.ctvit.wsservice.basedata.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.wsservice.basedata.domain.ZcdmTMonitor;
import com.ctvit.wsservice.basedata.domain.ZppmTProconrec;
import com.ctvit.wsservice.basedata.domain.ZppmTProginfo;
import com.ctvit.wsservice.basedata.domain.ZppmTProstock;
import com.ctvit.wsservice.basedata.domain.ZppmTProtech;
import com.ctvit.wsservice.basedata.domain.ZppmTask;
@SuppressWarnings("serial")
public class TaskFeedbackService extends BaseService {
	/**
	 * 
	 * @ps: 备播监控信息表保存
	 * @param zcdmTMonitor
	 * @param zcdmTMonitors
	 * @return -1 参数无效 0操作成功 1更新失败
	 */
	public int save(List<ZcdmTMonitor> zcdmTMonitors) {
		if (zcdmTMonitors == null)
			return -1;
		ZcdmTMonitor zcdmTMonitor = null;
		for (int i = 0; i < zcdmTMonitors.size(); i++) {
			zcdmTMonitor = zcdmTMonitors.get(i);
			Condition condition = new Condition();
			condition.add("zProItemCd", zcdmTMonitor.getZProItemCd());
			condition.add("zSystemId", zcdmTMonitor.getZSystemId());
			if (genericDao.updateByCondition(zcdmTMonitor, new String[] {
					"zProItemSta", "zReceiveDate", "zReceiveTime" }, condition) == 0) {
				Long pk = genericDao.insert(zcdmTMonitor);
				if (StringUtils.equals(zcdmTMonitor.getZSystemId(), "EB02")) {
					ZppmTProginfo zppmTProginfo = new ZppmTProginfo();
					zppmTProginfo.setZPlystat(zcdmTMonitor.getZProItemSta());
					condition = new Condition();
					condition.add("zProcode", zcdmTMonitor.getZProCode());
					condition.add("zAudVer", zcdmTMonitor.getZAudVer());
					condition.add("zCapVer", zcdmTMonitor.getZCapVer());
					genericDao.updateByCondition(zppmTProginfo,
							new String[] { "zPlystat" }, condition);
				}
			}
		}
		return 0;
	}

	/**
	 * 
	 * @ps:E2E:节目内审记录表保存
	 * @param zppmTProconrec
	 * @return
	 */
	public int saveProconrec(ZppmTProconrec zppmTProconrec) {
		int ret = 0;
		Condition condition = new Condition();
		condition.add("zTaskId", zppmTProconrec.getZTaskId());
		condition.add("zTaskName", "TaskReceiveService");
		ZppmTask zppmTask = genericDao.getObjectByCondition(ZppmTask.class,
				condition);
		if (zppmTask == null) {
			ret = -1;
		} else if (StringUtils.equals(zppmTask.getZConrecDone(), "X")) {
			ret = -2;// 任务已经内审完成
		} else {
			// 如果是内审最后一级则改为内审完成
			if (StringUtils.equals(zppmTProconrec.getZConStage(), "4")) {
				zppmTask.setZConrecDone("X");
				genericDao.update(zppmTask, new String[] { "zConrecDone" });

				ZppmTProginfo zppmTProginfo = new ZppmTProginfo();
				zppmTProginfo.setZProdstat("3");// 内审完成
				condition = new Condition();
				condition.add("zProcode", zppmTask.getZProcode());
				condition.add("zAudVer", zppmTask.getZAudVer());
				condition.add("zCapVer", zppmTask.getZCapVer());
				genericDao.updateByCondition(zppmTProginfo,
						new String[] { "zProdstat" }, condition);
			}
			Long pk = genericDao.insert(zppmTProconrec);
		}
		return ret;
	}

	/**
	 * 入库结果保存
	 * 
	 * @param zppmTProstock
	 * @return
	 */
	public int saveProRecStock(ZppmTProstock zppmTProstock) {
		int ret = 0;
		// 读取任务下达时的节目信息
		Condition condition = new Condition();
		condition.add("zTaskId", zppmTProstock.getZTaskId());
		condition.add("zTaskName", "TaskReceiveService");
		ZppmTask zppmTask = genericDao.getObjectByCondition(ZppmTask.class,
				condition);
		if (zppmTask == null) {
			ret = -1;
		} else if (StringUtils.equals(zppmTask.getZStockDone(), "X")) {
			ret = -2;// 任务已经入库
		} else {
			zppmTProstock.setZProcode(zppmTask.getZProcode());// 节目代码
			zppmTProstock.setZAudVer(zppmTask.getZAudVer());// 音频语言版本
			zppmTProstock.setZCapVer(zppmTask.getZCapVer());// 字幕语言版本
			Long pk = genericDao.insert(zppmTProstock);

			zppmTask.setZStockDone("X");
			genericDao.update(zppmTask, new String[] { "zStockDone" });

			ZppmTProginfo zppmTProginfo = new ZppmTProginfo();
			zppmTProginfo.setZProdstat("6");// 入库完成
			condition = new Condition();
			condition.add("zProcode", zppmTask.getZProcode());
			condition.add("zAudVer", zppmTask.getZAudVer());
			condition.add("zCapVer", zppmTask.getZCapVer());
			genericDao.updateByCondition(zppmTProginfo,
					new String[] { "zProdstat" }, condition);

			ret = 0;
		}
		return ret;
	}

	/**
	 * 技审结果保存
	 * 
	 * @param zppmTProtech
	 * @return
	 */
	public int saveProgTechResult(ZppmTProtech zppmTProtech) {
		int ret = 0;
		// 同一个节目代码，一个技审单号只能回一次技审完成
		Condition condition = new Condition();
		condition.add("zProcode", zppmTProtech.getZProcode());
		condition.add("zAudVer", zppmTProtech.getZAudVer());
		condition.add("zCapVer", zppmTProtech.getZCapVer());
		condition.add("zAuditBillid", zppmTProtech.getZAuditBillid());
		if (genericDao.updateByCondition(zppmTProtech, new String[] {
				"zProcode", "zAuditBillid" }, condition) == 0) {
			condition = new Condition();
			condition.add("zProcode", zppmTProtech.getZProcode());
			condition.add("zAudVer", zppmTProtech.getZAudVer());
			condition.add("zCapVer", zppmTProtech.getZCapVer());
			condition.add("zTechDone", "<>", "'X'",true);
			List<ZppmTask> tasklist = genericDao.getByCondition(ZppmTask.class,
					condition, null);
			if (tasklist != null && tasklist.size() == 1) {// 当前代码只有一个未完成技审的任务
				Long pk = genericDao.insert(zppmTProtech);
				ZppmTask zppmTask = tasklist.get(0);
				zppmTask.setZTechDone("X");
				genericDao.update(zppmTask, new String[] { "zTechDone" });
				ZppmTProginfo zppmTProginfo = new ZppmTProginfo();
				zppmTProginfo.setZProdstat("4");// 技审完成
				condition = new Condition();
				condition.add("zProcode", zppmTProtech.getZProcode());
				condition.add("zAudVer", zppmTProtech.getZAudVer());
				condition.add("zCapVer", zppmTProtech.getZCapVer());
				genericDao.updateByCondition(zppmTProginfo,
						new String[] { "zProdstat" }, condition);
			} else {
				ret = -2;
			}
		} else {
			ret = -1;
		}
		return ret;
	}
}
